using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Sql;
using System.Data.SqlClient;

/// <summary>
/// Summary description for Order
/// </summary>
public class OrderDAO : DBConnection
{
    #region sql command
    string SQL_SELECT_ID = "";
    string SQL_SELECT_ALL = "";
    string SQL_INSERT = "insert into [Order]([Date], CustomerID, CustomerName, Address, ReceiverAddress) values(@date, @customerId, @customerName, @address, @receiverAddress)";
    string SQL_UPDATE = "update [Order] set [Date] = @date, CustomerID = @customerId, CustomerName = @customerName, Address = @address, ReceiverAddress = @receiverAddress where OrderID = @orderId";
    string SQL_DELETE = "delete from [Order] where OrderID = @orderId";
    #endregion

    #region fields
    string message = "";
    #endregion

    #region constructors
    public OrderDAO()
    {
    }
    #endregion

    #region properties
    public string Message
    {
        get { return message; }       
    }
    #endregion

    #region methods
    public int InsertOrder(Order order)
    {
        int effectedRow = 0;
        try
        {
            SqlCommand cmd = new SqlCommand(SQL_INSERT, conn);
            cmd.Parameters.AddWithValue("@date", order.OrderDate);
            cmd.Parameters.AddWithValue("@customerId", order.Customer.ID);
            cmd.Parameters.AddWithValue("@customerName", order.Customer.Name);
            cmd.Parameters.AddWithValue("@address", order.Customer.Address);
            cmd.Parameters.AddWithValue("@receiverAddress", order.ReceiverAddress);
            if (Connect())
            {
                effectedRow = cmd.ExecuteNonQuery();
                cmd.CommandText = "select @@IDENTITY from [Order]";
                order.OrderID = int.Parse(cmd.ExecuteScalar().ToString());
                Disconnect();
            }
        }
        catch (SqlException ex) { message = ex.Message; }
        catch (Exception ex) { message = ex.Message; }
        return effectedRow;
    }

    public int UpdateOrder(Order order)
    {
        int effectedRow = 0;
        try
        {
            SqlCommand cmd = new SqlCommand(SQL_UPDATE, conn);
            cmd.Parameters.AddWithValue("@orderId", order.OrderID);
            cmd.Parameters.AddWithValue("@date", order.OrderDate);
            cmd.Parameters.AddWithValue("@customerId", order.Customer.ID);
            cmd.Parameters.AddWithValue("@customerName", order.Customer.Name);
            cmd.Parameters.AddWithValue("@address", order.Customer.Address);
            cmd.Parameters.AddWithValue("@receiverAddress", order.ReceiverAddress);
            if (Connect())
            {
                effectedRow = cmd.ExecuteNonQuery();
                Disconnect();
            }
        }
        catch (SqlException ex) { message = ex.Message; }
        catch (Exception ex) { message = ex.Message; }
        return effectedRow;
    }

    public int DeleteOrder(Order order)
    {
        int effectedRow = 0;
        try
        {
            SqlCommand cmd = new SqlCommand(SQL_DELETE, conn);
            cmd.Parameters.AddWithValue("@orderId", order.OrderID);
            if (Connect())
            {
                effectedRow = cmd.ExecuteNonQuery();
                Disconnect();
            }
        }
        catch (SqlException ex) { message = ex.Message; }
        catch (Exception ex) { message = ex.Message; }
        return effectedRow;
    }
    #endregion
}
